package net.techwatch.springtest.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import net.techwatch.springtest.dao.PersonDao;
import net.techwatch.springtest.domain.Person;

/**
 * @author wilfried
 *
 */
public class JdbcPersonDao implements PersonDao {
	
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * Default constructor.
	 */
	public JdbcPersonDao() {
		// nothing to do
	}

	/**
	 * @return the jdbcTemplate
	 */
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	/**
	 * @param jdbcTemplate the jdbcTemplate to set
	 */
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@SuppressWarnings("unchecked")
	public List<Person> selectAll() throws DataAccessException {
		return jdbcTemplate.query("SELECT * FROM person", new RowMapper() {
		
			public Object mapRow(ResultSet resultSet, int index) throws SQLException {
				Person person = new Person();
				person.setFirstName(resultSet.getString("first_name"));
				person.setLastName(resultSet.getString("last_name"));
				person.setAge(resultSet.getInt("age"));
				return null;
			}
		
		});
	}

}
